﻿#ifndef GATEWAYTCPCLIENT_H
#define GATEWAYTCPCLIENT_H

#include <QObject>
#include <QTimer>
#include <QHostAddress>
#include <QTcpSocket>
#include <QUdpSocket>
#include <QTcpServer>
#include <QThread>

#include "qbindtcpsocket.h"

class ConnectDetectThread;


class GatewayTcpClient : public QObject
{
    Q_OBJECT
public:
    explicit GatewayTcpClient(QObject *parent = NULL);
    virtual ~GatewayTcpClient();

public:
    void setConfigFileName(QString configfilename);
signals:
    void siConnectSta(int sta);          // 网络连接信号
    void siConnectErr(QString errinfo);  // 网络错误信息
    void siReceiveData(QByteArray dat);  // 收到数据包信号
signals:
    void siDetectHost(void);
public:


public slots:
    void slotSendData(QByteArray dat);  // 发送数据的槽
    void connectToServer();//连接到服务器
    void disConnectFromServer();
    void detectStatus(int);
public:

private slots:
    void connectCheck();
    void receiveServerData();
    void displaySocketError(QAbstractSocket::SocketError err);
    void netStateChanged(QAbstractSocket::SocketState sta);
    void hostFound();

private:
    void loadIpAndPort();
private:
    QBindTcpSocket * m_pClientSocket;

    QTimer * m_pConnectCheckTimer;

public:
    int m_connected;            // 连接状态; 0, 初始化; 1, 未连接; 2, 连接中; 3, 已连接

private:
    QThread * m_pConnectDetectThread;
    TcpConnectDetect * m_pConnectDetect;
    int m_detect;               // 连接测试状态
private:
    QString m_configFileName;
private:
    QString m_localip;
    quint16 m_localport;
    QString m_serverip;
    quint16 m_serverport;
};

#endif // GATEWAYTCPCLIENT_H
